from retrieval.process import *
from retrieval.retrieve import *
from retrieval.rate import RatingSystem


if __name__ == '__main__':
    texts = get_text()
    vec, X = get_vec(texts)
    inverse_table = gen_index(texts, vec, X.toarray())
    rate_sys = RatingSystem()

    print('检索系统正在运行。')
    while True:
        search_str = input('[搜索词句]: ')

        if search_str == 'q':
            print('已退出系统')
            exit(0)

        result = retrieve(search_str, inverse_table, texts, vec, X)
        # for index, item in enumerate(result):
        for i in range(0, int(len(result)/2)):
            print(result[i])
        more = input('<q> 退出, <any> 更多.......')
        if more == 'q':
            break
        else:
            for i in range(int(len(result)/2), len(result)):
                print(result[i])

        T = len(result)
        print(f'共检索出{T}篇文献')
        tp = int(input('检测出文献中，准确的篇数：'))
        fp = int(input('未检测文献中，准确的篇数：'))
        recall, precision, f1= RatingSystem.scores(T, tp, fp)
        print(f'准确率：{precision}，召回率：{recall}，f1-score：{f1}\n')
        score = float(input('请为此次搜索满意度打分[0-10]: '))
        RatingSystem.save('ratings.txt', score, search_str)
